﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.iss.inventory.Entity;

namespace Staionery_Inventory.Presentation.Reports
{
    public partial class StationeryRequistionTrend : System.Web.UI.Page
    {
        InvManagementEntities ime = new InvManagementEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["empID"] == null)
            {
                Response.Redirect("~/Presentation/login.aspx");
            }
            //assign user Name
            Label lbl = this.Master.FindControl("lblLogInName") as Label;
            if (lbl != null)
            {
                lbl.Text = Session["UserName"].ToString();
            }

            if (!IsPostBack)
            {
                var ca = (from s in ime.StationeryItems select s.Category).Distinct<string>();
                foreach (string s in ca)
                {
                    Category.Items.Add(s.ToString());
                }
                var si = (from s in ime.Departments select s.DeptName).Distinct<string>();
                foreach (string s in si)
                {
                    Ddldept.Items.Add(s.ToString());
                }
            }
                     
             }

        protected void rbCustoms_CheckedChanged(object sender, EventArgs e)
        {
            if (rbCustoms.Checked == true)
            {
                ddlMonth1.Enabled = true;
                ddlMonth2.Enabled = true;
                ddlMonth3.Enabled = true;
                ddlYear1.Enabled = true;
                ddlYear2.Enabled = true;
                ddlYear3.Enabled = true;
                Lblmonth1.Enabled = true;
                lblMonth3.Enabled = true;
                lblMonth2.Enabled = true;
                lblYear2.Enabled = true;
                lblYear3.Enabled = true;
                lblYear1.Enabled = true;
            }
           
           

               
        }

       

        protected void rbLast3Months_CheckedChanged(object sender, EventArgs e)
        {
            if (rbLast3Months.Checked == true)
            {
                ddlMonth1.Enabled = false;
                ddlMonth2.Enabled = false;
                ddlMonth3.Enabled = false;
                ddlYear1.Enabled = false;
                ddlYear2.Enabled = false;
                ddlYear3.Enabled = false;
                Lblmonth1.Enabled = false;
                lblMonth3.Enabled = false;
                lblMonth2.Enabled = false;
                lblYear2.Enabled = false;
                lblYear3.Enabled = false;
                lblYear1.Enabled = false;
            }


        }
        protected void rbQuantity_CheckedChanged(object sender, EventArgs e)
        {
            if (rbQuantity.Checked == true)
            {
                Ddldept.Enabled = true;
                rbLast3Months.Enabled = true;
                rbCustoms.Enabled = true;
                lblDepartment.Enabled = true;
                lbloptions.Enabled = true;

            }
        }

        protected void Inventory_CheckedChanged(object sender, EventArgs e)
        {
            if (rbInventory.Checked == true)
            {
                Ddldept.Enabled = false;
                rbLast3Months.Enabled = false;
                rbCustoms.Enabled = false;
                lblDepartment.Enabled = false;
                lbloptions.Enabled = false;

            }
        }

        protected void btngeneratereport_Click(object sender, EventArgs e)
        {
            if (rbInventory.Checked == true)
            {
                 List<report> list = new List<report>();

                InvManagementEntities inventent = new InvManagementEntities();
                List<String> li = new List<string>();
                List<report> data = new List<report>();
                foreach (ListItem l in Category.Items)
                    if (l.Selected)
                    {
                        li.Add(l.Value);
                        report temp_data = new report();
                        temp_data.Category = l.Value;
                        temp_data.QtyLeft = 0;
                        data.Add(temp_data);
                    }
                // List<object> list = new List<object>();
                //InvManagementEntities inventent = new InvManagementEntities();
                var t = from inv in inventent.StationeryItems
                        group inv by inv.Category into d
                        select new report
                        {
                            Category = d.Key,
                            QtyLeft = (int)d.Sum(x => x.QtyLeft)
                        };
                list = t.ToList<report>();
                data.Clear();
                foreach (ListItem l in Category.Items)
                    if (l.Selected)
                    {
                        li.Add(l.Value);
                        report temp_data = new report();
                        temp_data.Category = l.Value;
                        temp_data.QtyLeft = 0;
                        data.Add(temp_data);
                    }

                foreach (report l_rpt in data)
                {
                    foreach (report o in list)
                    {
                        if (l_rpt.Category == o.Category)
                            l_rpt.QtyLeft = o.QtyLeft;
                    }
                }

                if (list.Count != 0)
                {

                    Chart1.DataSource = data;
                    Chart1.Series["Series1"].XValueMember = "Category";
                    Chart1.Series["Series1"].YValueMembers = "QtyLeft";
                    Chart1.DataBind();
                }

            }
             else if (rbQuantity.Checked == true)
            {

                if (rbLast3Months.Checked == true)
                {
                    List<report> list = new List<report>();
                    InvManagementEntities inventent = new InvManagementEntities();
                    List<String> li = new List<string>();
                    List<report> data = new List<report>();
                    foreach (ListItem l in Category.Items)
                        if (l.Selected)
                        {
                            li.Add(l.Value);
                            report temp_data = new report();
                            temp_data.Category = l.Value;
                            temp_data.QtyRetrieved = 0;
                            data.Add(temp_data);
                        }

                    {

                        int month = System.DateTime.Now.Month;

                        DateTime selectedatetime = new DateTime(2014, month, 1);
                        DateTime startdate = new DateTime(2014, month, 1);
                        DateTime enddate = new DateTime();

                        var thismonth = startdate.AddDays(1 - selectedatetime.Day);
                        startdate = Convert.ToDateTime(thismonth);
                        var thismonend = thismonth.AddMonths(1).AddSeconds(-1);
                        enddate = Convert.ToDateTime(thismonend);


                        var q = (from abcd in inventent.SRFDetails
                                 join abcde in inventent.StationeryItems on abcd.ItemID equals abcde.ItemID
                                 where li.Contains(abcd.StationeryItem.Category) && abcd.Department.DeptName == Ddldept.SelectedValue && (abcd.SRF.RFDate >= startdate && abcd.SRF.RFDate <= enddate)
                                 group abcd by abcd.StationeryItem.Category into d
                                 select new report
                                 {
                                     Category = d.Key,
                                     QtyRetrieved = (int)d.Sum(x => x.QtyRetrieved)
                                 });
                        list = q.ToList<report>();
                        data.Clear();
                        foreach (ListItem l in Category.Items)
                            if (l.Selected)
                            {
                                li.Add(l.Value);
                                report temp_data = new report();
                                temp_data.Category = l.Value;
                                temp_data.QtyRetrieved = 0;
                                data.Add(temp_data);
                            }

                        foreach (report l_rpt in data)
                        {
                            foreach (report o in list)
                            {
                                if (l_rpt.Category == o.Category)
                                    l_rpt.QtyRetrieved = o.QtyRetrieved;
                            }
                        }

                        if (list.Count != 0)
                        {

                            Chart1.DataSource = data;
                            Chart1.Series["Series1"].XValueMember = "Category";
                            Chart1.Series["Series1"].YValueMembers = "QtyRetrieved";
                            Chart1.DataBind();
                        }

                    }
                    {

                        int month = System.DateTime.Now.Month - 1;
                        DateTime selectedatetime = new DateTime(2014, month, 1);
                        DateTime startdate = new DateTime(2014, month, 1);
                        DateTime enddate = new DateTime();

                        var thismonth = startdate.AddDays(1 - selectedatetime.Day);
                        startdate = Convert.ToDateTime(thismonth);
                        var thismonend = thismonth.AddMonths(1).AddSeconds(-1);
                        enddate = Convert.ToDateTime(thismonend);


                        var q = (from abcd in inventent.SRFDetails
                                 join abcde in inventent.StationeryItems on abcd.ItemID equals abcde.ItemID
                                 where li.Contains(abcd.StationeryItem.Category) && abcd.Department.DeptName == Ddldept.SelectedValue && (abcd.SRF.RFDate >= startdate && abcd.SRF.RFDate <= enddate)
                                 group abcd by abcd.StationeryItem.Category into d
                                 select new report
                                 {
                                     Category = d.Key,
                                     QtyRetrieved = (int)d.Sum(x => x.QtyRetrieved)
                                 });


                        list = q.ToList<report>();
                        data.Clear();
                        foreach (ListItem l in Category.Items)
                            if (l.Selected)
                            {
                                li.Add(l.Value);
                                report temp_data = new report();
                                temp_data.Category = l.Value;
                                temp_data.QtyRetrieved = 0;
                                data.Add(temp_data);
                            }

                        foreach (report l_rpt in data)
                        {
                            foreach (report o in list)
                            {
                                if (l_rpt.Category == o.Category)
                                    l_rpt.QtyRetrieved = o.QtyRetrieved;
                            }
                        }

                        if (list.Count != 0)
                        {

                            Chart2.DataSource = data;
                            Chart2.Series["Series1"].XValueMember = "Category";
                            Chart2.Series["Series1"].YValueMembers = "QtyRetrieved";
                            Chart2.DataBind();
                        }
                    }

                    {

                        int month = System.DateTime.Now.Month - 2;
                        DateTime selectedatetime = new DateTime(2014, month, 1);
                        DateTime startdate = new DateTime(2014, month, 1);
                        DateTime enddate = new DateTime();

                        var thismonth = startdate.AddDays(1 - selectedatetime.Day);
                        startdate = Convert.ToDateTime(thismonth);
                        var thismonend = thismonth.AddMonths(1).AddSeconds(-1);
                        enddate = Convert.ToDateTime(thismonend);


                        var q = (from abcd in inventent.SRFDetails
                                 join abcde in inventent.StationeryItems on abcd.ItemID equals abcde.ItemID
                                 where li.Contains(abcd.StationeryItem.Category) && abcd.Department.DeptName == Ddldept.SelectedValue && (abcd.SRF.RFDate >= startdate && abcd.SRF.RFDate <= enddate)
                                 group abcd by abcd.StationeryItem.Category into d
                                 select new report
                                 {
                                     Category = d.Key,
                                     QtyRetrieved = (int)d.Sum(x => x.QtyRetrieved)
                                 });

                        list = q.ToList<report>();
                        data.Clear();
                        foreach (ListItem l in Category.Items)
                            if (l.Selected)
                            {
                                li.Add(l.Value);
                                report temp_data = new report();
                                temp_data.Category = l.Value;
                                temp_data.QtyRetrieved = 0;
                                data.Add(temp_data);
                            }

                        foreach (report l_rpt in data)
                        {
                            foreach (report o in list)
                            {
                                if (l_rpt.Category == o.Category)
                                    l_rpt.QtyRetrieved = o.QtyRetrieved;
                            }
                        }

                        if (list.Count != 0)
                        {

                            Chart3.DataSource = data;
                            Chart3.Series["Series1"].XValueMember = "Category";
                            Chart3.Series["Series1"].YValueMembers = "QtyRetrieved";
                            Chart3.DataBind();
                        }


                    }




                }
                else if (rbCustoms.Checked == true)
                {
                    List<report> list = new List<report>();
                    InvManagementEntities inventent = new InvManagementEntities();
                    List<String> li = new List<string>();
                    List<report> data = new List<report>();
                    foreach (ListItem l in Category.Items)
                        if (l.Selected)
                        {
                            li.Add(l.Value);
                            report temp_data = new report();
                            temp_data.Category = l.Value;
                            temp_data.QtyRetrieved = 0;
                            data.Add(temp_data);
                        }
                    {
                        string selectedMonth = ddlMonth1.SelectedValue;
                        int month = Convert.ToInt32(selectedMonth);
                        string selectedyear = ddlYear1.SelectedValue;
                        int year = Convert.ToInt32(selectedyear);

                        DateTime selectedDateTime = new DateTime(year, month, 1);
                        DateTime startDate = new DateTime(year, month, 1);
                        DateTime endDate = new DateTime();
                        var thisMonthStart = startDate.AddDays(1 - selectedDateTime.Day);
                        startDate = Convert.ToDateTime(thisMonthStart);
                        var thisMonthEnd = thisMonthStart.AddMonths(1).AddSeconds(-1);
                        endDate = Convert.ToDateTime(thisMonthEnd);
                        var q = (from abcd in inventent.SRFDetails
                                 join abcde in inventent.StationeryItems on abcd.ItemID equals abcde.ItemID
                                 where li.Contains(abcd.StationeryItem.Category) && abcd.Department.DeptName == Ddldept.SelectedValue && (abcd.SRF.RFDate >= startDate && abcd.SRF.RFDate <= endDate)
                                 group abcd by abcd.StationeryItem.Category into d
                                 select new report
                                 {
                                     Category = d.Key,
                                     QtyRetrieved = (int)d.Sum(x => x.QtyRetrieved)
                                 });
                        list = q.ToList<report>();
                        data.Clear();
                        foreach (ListItem l in Category.Items)
                            if (l.Selected)
                            {
                                li.Add(l.Value);
                                report temp_data = new report();
                                temp_data.Category = l.Value;
                                temp_data.QtyRetrieved = 0;
                                data.Add(temp_data);
                            }

                        foreach (report l_rpt in data)
                        {
                            foreach (report o in list)
                            {
                                if (l_rpt.Category == o.Category)
                                    l_rpt.QtyRetrieved = o.QtyRetrieved;
                            }
                        }

                        if (list.Count != 0)
                        {

                            Chart1.DataSource = data;
                            Chart1.Series["Series1"].XValueMember = "Category";
                            Chart1.Series["Series1"].YValueMembers = "QtyRetrieved";
                            Chart1.DataBind();
                        }

                    }
                    {
                        string selectedMonth = ddlMonth2.SelectedValue;
                        int month = Convert.ToInt32(selectedMonth);
                        string selectedyear = ddlYear2.SelectedValue;
                        int year = Convert.ToInt32(selectedyear);

                        DateTime selectedDateTime = new DateTime(year, month, 1);
                        DateTime startDate = new DateTime(year, month, 1);
                        DateTime endDate = new DateTime();
                        var thisMonthStart = startDate.AddDays(1 - selectedDateTime.Day);
                        startDate = Convert.ToDateTime(thisMonthStart);
                        var thisMonthEnd = thisMonthStart.AddMonths(1).AddSeconds(-1);
                        endDate = Convert.ToDateTime(thisMonthEnd);
                        var q = (from abcd in inventent.SRFDetails
                                 join abcde in inventent.StationeryItems on abcd.ItemID equals abcde.ItemID
                                 where li.Contains(abcd.StationeryItem.Category) && abcd.Department.DeptName == Ddldept.SelectedValue && (abcd.SRF.RFDate >= startDate && abcd.SRF.RFDate <= endDate)
                                 group abcd by abcd.StationeryItem.Category into d
                                 select new report
                                 {
                                     Category = d.Key,
                                     QtyRetrieved = (int)d.Sum(x => x.QtyRetrieved)
                                 });
                        list = q.ToList<report>();
                        data.Clear();
                        foreach (ListItem l in Category.Items)
                            if (l.Selected)
                            {
                                li.Add(l.Value);
                                report temp_data = new report();
                                temp_data.Category = l.Value;
                                temp_data.QtyRetrieved = 0;
                                data.Add(temp_data);
                            }

                        foreach (report l_rpt in data)
                        {
                            foreach (report o in list)
                            {
                                if (l_rpt.Category == o.Category)
                                    l_rpt.QtyRetrieved = o.QtyRetrieved;
                            }
                        }

                        if (list.Count != 0)
                        {

                            Chart2.DataSource = data;
                            Chart2.Series["Series1"].XValueMember = "Category";
                            Chart2.Series["Series1"].YValueMembers = "QtyRetrieved";
                            Chart2.DataBind();
                        }

                    }
                    {
                        string selectedMonth = ddlMonth3.SelectedValue;
                        int month = Convert.ToInt32(selectedMonth);
                        string selectedyear = ddlYear3.SelectedValue;
                        int year = Convert.ToInt32(selectedyear);

                        DateTime selectedDateTime = new DateTime(year, month, 1);
                        DateTime startDate = new DateTime(year, month, 1);
                        DateTime endDate = new DateTime();
                        var thisMonthStart = startDate.AddDays(1 - selectedDateTime.Day);
                        startDate = Convert.ToDateTime(thisMonthStart);
                        var thisMonthEnd = thisMonthStart.AddMonths(1).AddSeconds(-1);
                        endDate = Convert.ToDateTime(thisMonthEnd);
                        var q = (from abcd in inventent.SRFDetails
                                 join abcde in inventent.StationeryItems on abcd.ItemID equals abcde.ItemID
                                 where li.Contains(abcd.StationeryItem.Category) && abcd.Department.DeptName == Ddldept.SelectedValue && (abcd.SRF.RFDate >= startDate && abcd.SRF.RFDate <= endDate)
                                 group abcd by abcd.StationeryItem.Category into d
                                 select new report
                                 {
                                     Category = d.Key,
                                     QtyRetrieved = (int)d.Sum(x => x.QtyRetrieved)
                                 });
                        list = q.ToList<report>();
                        data.Clear();
                        foreach (ListItem l in Category.Items)
                            if (l.Selected)
                            {
                                li.Add(l.Value);
                                report temp_data = new report();
                                temp_data.Category = l.Value;
                                temp_data.QtyRetrieved = 0;
                                data.Add(temp_data);
                            }

                        foreach (report l_rpt in data)
                        {
                            foreach (report o in list)
                            {
                                if (l_rpt.Category == o.Category)
                                    l_rpt.QtyRetrieved = o.QtyRetrieved;
                            }
                        }

                        if (list.Count != 0)
                        {

                            Chart3.DataSource = data;
                            Chart3.Series["Series1"].XValueMember = "Category";
                            Chart3.Series["Series1"].YValueMembers = "QtyRetrieved";
                            Chart3.DataBind();
                        }


                    }



                }

            }
        }

       

       
    }
}